package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController//相当于Controller+RespondBody注解，使用后类中方法省略两个注解
//@Controller
//指定所有方法的开始路径，每个方法的路径与该部分进行拼接
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("insert")
    //@ResponseBody
    public JsonResult adduser(UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);//复制类对象到另一个类
        /*user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());*/
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }

    @GetMapping("userList")
    //@ResponseBody
    public JsonResult userList(){
        return JsonResult.ok(userMapper.userList());
    }

    @GetMapping("delete")
    //@ResponseBody
    public JsonResult userDelete(Integer id){
        userMapper.userDelete(id);
        return JsonResult.ok();
    }
    @PostMapping("update")
   //@ResponseBody
    public JsonResult userUpdate(User user){
        userMapper.update(user);
        return JsonResult.ok();
    }
    @GetMapping("select/{id}")
    public JsonResult select(@PathVariable Integer id){
        if(id<0){
            throw new IllegalArgumentException("用户id不能小于0");
        }
        return JsonResult.ok(userMapper.select(id));
    }
}
